Attribute VB_Name = "modBitmap"
Option Explicit

Public Type PieceColor
        Tint As Long
        Middle As Long
        Fuscous As Long
End Type

Public Declare Function GetPixel Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long) As Long
Public Declare Function SetPixel Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal crColor As Long) As Long

Public PiecesColor(7) As PieceColor

Public Sub FillPieceColor(Board As GameBoard, Optional Changed As Boolean = False, Optional pos As Integer)

    Dim i As Integer, j As Integer, n As Integer, m As Integer
    Dim x1 As Integer, x2 As Integer, y1 As Integer, y2 As Integer
    Dim Color As Long
    Dim BoardDC As Long, OldBoardDC As Long

    n = Board.Level - (Board.Level \ 8) * 8

    If Changed Then
        frmMain.picTemp.Cls
        Select Case pos
            Case 0
                Exit Sub
            Case 1
                x1 = 0: x2 = STEP - 1
                y1 = 0: y2 = STEP - 1
                frmMain.picTemp.Picture = frmMain.picDot1.Image
            Case 20
                x1 = 0: x2 = 2 * STEP - 1
                y1 = 0: y2 = STEP - 1
                frmMain.picTemp.Picture = frmMain.picDot2(0).Image
            Case 21
                x1 = 0: x2 = STEP - 1
                y1 = 0: y2 = 2 * STEP - 1
                frmMain.picTemp.Picture = frmMain.picDot2(1).Image
            Case 30
                x1 = 0: x2 = 3 * STEP - 1
                y1 = 0: y2 = STEP - 1
                frmMain.picTemp.Picture = frmMain.picDot3(0).Image
            Case 31
                x1 = 0: x2 = STEP - 1
                y1 = 0: y2 = 3 * STEP - 1
                frmMain.picTemp.Picture = frmMain.picDot3(1).Image
            Case 320
                x1 = 0: x2 = 2 * STEP - 1
                y1 = 0: y2 = 2 * STEP - 1
                frmMain.picTemp.Picture = frmMain.picDot3z(0).Image
            Case 321
                x1 = 0: x2 = 2 * STEP - 1
                y1 = 0: y2 = 2 * STEP - 1
                frmMain.picTemp.Picture = frmMain.picDot3z(1).Image
            Case 322
                x1 = 0: x2 = 2 * STEP - 1
                y1 = 0: y2 = 2 * STEP - 1
                frmMain.picTemp.Picture = frmMain.picDot3z(2).Image
            Case 323
                x1 = 0: x2 = 2 * STEP - 1
                y1 = 0: y2 = 2 * STEP - 1
                frmMain.picTemp.Picture = frmMain.picDot3z(3).Image
        End Select

        For i = x1 To x2
            For j = y1 To y2
                Color = GetPixel(frmMain.picTemp.hdc, i, j)
                If Color <> vbBlack Then
                    If Color = PiecesColor(0).Tint Then SetPixel frmMain.picTemp.hdc, i, j, PiecesColor(n).Tint
                    If Color = PiecesColor(0).Middle Then SetPixel frmMain.picTemp.hdc, i, j, PiecesColor(n).Middle
                    If Color = PiecesColor(0).Fuscous Then SetPixel frmMain.picTemp.hdc, i, j, PiecesColor(n).Fuscous
                End If
            Next
        Next
    Else
        Select Case Board.CurPiece
            Case 1
                Select Case Board.PiecePos
                    Case 1
                        x1 = 0: x2 = 4 * STEP - 1
                        y1 = 0: y2 = STEP - 1
                        frmMain.picTemp.Picture = frmMain.picPiecesGray1(0).Image
                    Case 2
                        x1 = 0: x2 = STEP - 1
                        y1 = 0: y2 = 4 * STEP - 1
                        frmMain.picTemp.Picture = frmMain.picPiecesGray2(0).Image
                End Select
            Case 2
                x1 = 0: x2 = 2 * STEP - 1
                y1 = 0: y2 = 2 * STEP - 1
                frmMain.picTemp.Picture = frmMain.picPiecesGray1(1).Image
            Case 3
                Select Case Board.PiecePos
                    Case 1
                        x1 = 0: x2 = 3 * STEP - 1
                        y1 = 0: y2 = 2 * STEP - 1
                        frmMain.picTemp.Picture = frmMain.picPiecesGray1(2).Image
                    Case 2
                        x1 = 0: x2 = 2 * STEP - 1
                        y1 = 0: y2 = 3 * STEP - 1
                        frmMain.picTemp.Picture = frmMain.picPiecesGray2(2).Image
                    Case 3
                        x1 = 0: x2 = 3 * STEP - 1
                        y1 = 0: y2 = 2 * STEP - 1
                        frmMain.picTemp.Picture = frmMain.picPiecesGray3(2).Image
                    Case 4
                        x1 = 0: x2 = 2 * STEP - 1
                        y1 = 0: y2 = 3 * STEP - 1
                        frmMain.picTemp.Picture = frmMain.picPiecesGray4(2).Image
                End Select
            Case 4
                Select Case Board.PiecePos
                    Case 1
                        x1 = 0: x2 = 3 * STEP - 1
                        y1 = 0: y2 = 2 * STEP - 1
                        frmMain.picTemp.Picture = frmMain.picPiecesGray1(3).Image
                    Case 2
                        x1 = 0: x2 = 2 * STEP - 1
                        y1 = 0: y2 = 3 * STEP - 1
                        frmMain.picTemp.Picture = frmMain.picPiecesGray2(3).Image
                    Case 3
                        x1 = 0: x2 = 3 * STEP - 1
                        y1 = 0: y2 = 2 * STEP - 1
                        frmMain.picTemp.Picture = frmMain.picPiecesGray3(3).Image
                    Case 4
                        x1 = 0: x2 = 2 * STEP - 1
                        y1 = 0: y2 = 3 * STEP - 1
                        frmMain.picTemp.Picture = frmMain.picPiecesGray4(3).Image
                End Select
            Case 5
                Select Case Board.PiecePos
                    Case 1
                        x1 = 0: x2 = 3 * STEP - 1
                        y1 = 0: y2 = 2 * STEP - 1
                        frmMain.picTemp.Picture = frmMain.picPiecesGray1(4).Image
                    Case 2
                        x1 = 0: x2 = 2 * STEP - 1
                        y1 = 0: y2 = 3 * STEP - 1
                        frmMain.picTemp.Picture = frmMain.picPiecesGray2(4).Image
                    Case 3
                        x1 = 0: x2 = 3 * STEP - 1
                        y1 = 0: y2 = 2 * STEP - 1
                        frmMain.picTemp.Picture = frmMain.picPiecesGray3(4).Image
                    Case 4
                        x1 = 0: x2 = 2 * STEP - 1
                        y1 = 0: y2 = 3 * STEP - 1
                        frmMain.picTemp.Picture = frmMain.picPiecesGray4(4).Image
                End Select
            Case 6
                Select Case Board.PiecePos
                    Case 1
                        x1 = 0: x2 = 3 * STEP - 1
                        y1 = 0: y2 = 2 * STEP - 1
                        frmMain.picTemp.Picture = frmMain.picPiecesGray1(5).Image
                    Case 2
                        x1 = 0: x2 = 2 * STEP - 1
                        y1 = 0: y2 = 3 * STEP - 1
                        frmMain.picTemp.Picture = frmMain.picPiecesGray2(5).Image
                End Select
            Case 7
                Select Case Board.PiecePos
                    Case 1
                        x1 = 0: x2 = 3 * STEP - 1
                        y1 = 0: y2 = 2 * STEP - 1
                        frmMain.picTemp.Picture = frmMain.picPiecesGray1(6).Image
                    Case 2
                        x1 = 0: x2 = 2 * STEP - 1
                        y1 = 0: y2 = 3 * STEP - 1
                        frmMain.picTemp.Picture = frmMain.picPiecesGray2(6).Image
                End Select
        End Select

        For i = x1 To x2
            For j = y1 To y2
                Color = GetPixel(frmMain.picTemp.hdc, i, j)
                If Color <> vbBlack Then
                    If Color = PiecesColor(0).Tint Then SetPixel frmMain.picTemp.hdc, i, j, PiecesColor(n).Tint
                    If Color = PiecesColor(0).Middle Then SetPixel frmMain.picTemp.hdc, i, j, PiecesColor(n).Middle
                    If Color = PiecesColor(0).Fuscous Then SetPixel frmMain.picTemp.hdc, i, j, PiecesColor(n).Fuscous
                End If
            Next
        Next

        frmMain.picP1Board.Picture = frmMain.picP1OldBoard.Image
        Call RefreshDC(Board, BoardDC, OldBoardDC)

        Select Case Board.CurPiece
            Case 1
                Select Case Board.PiecePos
                    Case 1
                        BitBlt BoardDC, (Board.PieceX - 1) * STEP + 1, (Board.PieceY - 1) * STEP + 1, 4 * STEP, STEP, frmMain.picTemp.hdc, 0, 0, vbSrcInvert
                    Case 2
                        BitBlt BoardDC, (Board.PieceX - 1) * STEP + 1, (Board.PieceY - 1) * STEP + 1, STEP, 4 * STEP, frmMain.picTemp.hdc, 0, 0, vbSrcInvert
                End Select
            Case 2
                BitBlt BoardDC, (Board.PieceX - 1) * STEP + 1, (Board.PieceY - 1) * STEP + 1, 2 * STEP, 2 * STEP, frmMain.picTemp.hdc, 0, 0, vbSrcInvert
            Case 3, 4, 5, 6, 7
                Select Case Board.PiecePos
                    Case 1, 3
                        BitBlt BoardDC, (Board.PieceX - 1) * STEP + 1, (Board.PieceY - 1) * STEP + 1, 3 * STEP, 2 * STEP, frmMain.picTemp.hdc, 0, 0, vbSrcInvert
                    Case 2, 4
                        BitBlt BoardDC, (Board.PieceX - 1) * STEP + 1, (Board.PieceY - 1) * STEP + 1, 2 * STEP, 3 * STEP, frmMain.picTemp.hdc, 0, 0, vbSrcInvert
                End Select
        End Select

        frmMain.picP1Board.Refresh
    End If

End Sub

Public Sub FillLevelColor(Board As GameBoard)

    Dim i As Integer, j As Integer, m As Integer, n As Integer
    Dim Color As Long
    Dim BoardDC As Long, OldBoardDC As Long

    m = Board.Level - (Board.Level \ 8) * 8
    n = m
    If n = 0 Then n = 8

    For j = 0 To BoardHeight * STEP - 1
        For i = 0 To BoardWidth * STEP - 1
            Color = GetPixel(BoardDC, i, j)
            If Color <> vbBlack Then
                If Color = PiecesColor(n - 1).Tint Then SetPixel BoardDC, i, j, PiecesColor(m).Tint
                If Color = PiecesColor(n - 1).Middle Then SetPixel BoardDC, i, j, PiecesColor(m).Middle
                If Color = PiecesColor(n - 1).Fuscous Then SetPixel BoardDC, i, j, PiecesColor(m).Fuscous
            End If
        Next
        Call RefreshDC(Board, BoardDC, OldBoardDC)
    Next

    frmMain.picP1Board.Refresh

End Sub

Public Sub EraseLineClip(Board As GameBoard, Levels As String)

    Dim i As Integer, j As Integer, k As Integer, l As Integer
    Dim Color As Long
    Dim BoardDC As Long, OldBoardDC As Long
    Dim Level() As String

    If Board.P1 Then
        BoardDC = frmMain.picP1Board.hdc
        OldBoardDC = frmMain.picP1OldBoard.hdc
    ElseIf Board.P2 Then
        'BoardDC = frmMain.picP2Board.hdc
        'OldBoardDC = frmMain.picP2OldBoard.hdc
    End If

    Level = Split(Levels, ",")
    For i = -5 To 10
        For k = 0 To 6 * STEP - 1
            For l = 0 To STEP - 1
                Color = GetPixel(frmMain.picClip.hdc, k, l)
                If Color <> vbBlack Then
                    For j = 0 To UBound(Level)
                        SetPixel BoardDC, i * STEP + k, CInt(Level(j)) * STEP + l + 1, vbBlack
                    Next
                End If
            Next
        Next
        frmMain.picP1Board.Refresh
        Board.Game = False
        Delay 0.03, True
        'Delay 0.3, True
        Board.Game = True
    Next

End Sub

Public Sub RefreshDC(Board As GameBoard, ByRef BoardDC As Long, ByRef OldBoardDC As Long)

    If Board.P1 Then
        BoardDC = frmMain.picP1Board.hdc
        OldBoardDC = frmMain.picP1OldBoard.hdc
    End If

    If Board.P2 Then
        'BoardDC = frmMain.picP2Board.hdc
        'OldBoardDC = frmMain.picP2OldBoard.hdc
    End If

End Sub
